package com.jrx.anytxn.transaction.controller.cms;

import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.transaction.bean.LoanAbnormalQueueBean;
import com.jrx.anytxn.transaction.dto.loan.CancelOrderQueryRes;
import com.jrx.anytxn.transaction.service.IDisbursementLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.model.User;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 放款异常队列
 * @Author yhy
 * @Date 2023/5/8 14:27
 * @Version V1.0
 */
@Api(tags = "放款异常队列")
@RestController
@RequestMapping(value = "/transaction/abnormalQueue")
public class LoanAbnormalQueueController {
    private static final Logger logger = LoggerFactory.getLogger(LoanAbnormalQueueController.class);

    @Autowired
    private IDisbursementLogService disbursementLogService;

    @Autowired
    private InfoService infoService;

    @Autowired
    private IOrganizationTableService iOrganizationTableService;

    @ApiOperation(value = "放款异常队列分页查询", notes = "放款异常队列分页查询API")
    @GetMapping(value = "/getAbnormalList")
    public TxnRespResult<TxnPage> getAbnormalQueueWithPage(@RequestBody LoanAbnormalQueueBean abnormalQueueBean) {
        TxnRespResult<TxnPage> result = new TxnRespResult();
        try {
            result = result.getSuccess(disbursementLogService.queryLoanAbnormalQueue(abnormalQueueBean));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), TxnRespCode.BIZ_ERROR.getUserTips());
        }
        return result;
    }

    @ApiOperation(value = "撤销订单", notes = "撤销订单API")
    @PostMapping(value = "/cancelOrder")
    public TxnRespResult<CancelOrderQueryRes> cancelOrder (@RequestBody LoanAbnormalQueueBean abnormalQueueBean){
        TxnRespResult<CancelOrderQueryRes> result = new TxnRespResult<>();
        try {
            User user = infoService.getUser();
            PrOrganizationTable organization = iOrganizationTableService.findByOrId(Constant.DEFAULT_ORG_NUMBER, abnormalQueueBean.getCustomerId());
            Date businessDate = organization.getNextProcessingDate();
            String nickName = user.getNickName();
            CancelOrderQueryRes res = disbursementLogService.cancelOrder(businessDate, abnormalQueueBean.getOperStatus(), abnormalQueueBean.getSeqNo(), abnormalQueueBean.getTenantId(), nickName);
            result.getSuccess(res);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result.getFail(TxnRespCode.ERROR.getCode(), TxnRespCode.ERROR.getUserTips());
        }
        return result;
    }

    @ApiOperation(value = "放款结果查询重试", notes = "放款结果查询重试API")
    @PostMapping(value = "/queryRetry")
    public TxnRespResult<Boolean> queryRetry (@RequestBody LoanAbnormalQueueBean abnormalQueueBean){
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        try {
            String nickName = infoService.getUser().getNickName();
            disbursementLogService.queryRetry(abnormalQueueBean.getDisId(), nickName);
            result.getSuccess(true);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result.getFail(TxnRespCode.ERROR.getCode(), TxnRespCode.ERROR.getUserTips());
        }
        return result;
    }
}
